<!-- 单位编辑弹窗 -->
<template>
  <el-dialog
    :title="isUpdate?'修改单位':'添加单位'"
    :visible="visible"
    width="550px"
    :destroy-on-close="true"
    :lock-scroll="false"
    @update:visible="updateVisible">
    <el-form
      ref="form"
      :model="form"
      :rules="rules"
      label-width="100px">

      <el-form-item
        label="单位编号:"
        prop="unit_num">
        <el-input
          :maxlength="20"
          v-model="form.unit_num"
          placeholder="请输入单位编号"
        />
      </el-form-item>

      <el-form-item
        label="单位名称:"
        prop="unit_name">
        <el-input
          :maxlength="20"
          v-model="form.unit_name"
          placeholder="请输入单位名称"
          clearable/>
      </el-form-item>
      <el-form-item
        label="超管账号:"
        prop="create_user">
        <el-select
          v-model="form.create_user"
          placeholder="请选择"
          :popper-append-to-body="false"
          @remove-tag="removeTag"
        >
          <el-option
            v-for="item in user"
            :key="item.id"
            :label="item.realname"
            :value="item.id"
          >
          </el-option>
        </el-select>
      </el-form-item>
      <el-form-item label="单位地址：" prop="area_id">
        <el-cascader
          clearable
          v-model="form.area_id"
          class="ele-fluid"
          placeholder="请选择省市区"
          :options="regions"
          popper-class="ele-pop-wrap-higher"
          @change="changeArea"
        />
        <el-input v-model="form.unit_address" placeholder="请输入详细地址"></el-input>
      </el-form-item>
      <el-form-item
        label="所属模块:"
        prop="model_type">
        <el-select
          v-model="form.model_type"
          multiple
          placeholder="请选择"
          :popper-append-to-body="false"
          @remove-tag="removeTag"
        >
          <el-option
            v-for="item in options"
            :key="item.value"
            :label="item.label"
            :value="item.value"
          >
          </el-option>
        </el-select>

      </el-form-item>

      <el-form-item label="状态:">
        <el-radio-group
          v-model="form.status">
                  <el-radio :label="1">停用</el-radio>
                  <el-radio :label="2">启用</el-radio>
                </el-radio-group>
      </el-form-item>

      <el-form-item
        label="备注:"
        prop="remark">
        <el-input
          :maxlength="20"
          v-model="form.remark"
          placeholder="请输入备注"
          clearable/>
      </el-form-item>
                </el-form>
    <div slot="footer">
      <el-button @click="updateVisible(false)">取消</el-button>
      <el-button
        type="primary"
        @click="save"
        :loading="loading">保存
      </el-button>
    </div>
  </el-dialog>
</template>

<script>

// import regions from 'ele-admin/packages/regions';
import { regionData } from 'element-china-area-data'
export default {
  name: 'UnitEdit',
  props: {
    // 弹窗是否打开
    visible: Boolean,
    // 修改回显的数据
    data: Object
  },
  data() {
    return {
      regions: regionData,
      // 表单数据
      form: Object.assign({status:1}, this.data),
      // 表单验证规则
      rules: {
              unit_num: [
          {required: true, message: '请输入单位编号', trigger: 'blur'}
        ],
        model_type: [
          {required: true, message: '请选择模块', trigger: 'blur'}
        ],
                  unit_name: [
          {required: true, message: '请输入单位名称', trigger: 'blur'}
        ],
                  unit_address: [
          {required: true, message: '请输入单位地址', trigger: 'blur'}
        ],
        status: [
          {required: true, message: '请选择状态', trigger: 'blur'}
        ],
        //                   remark: [
        //   {required: true, message: '请输入备注', trigger: 'blur'}
        // ],
            },
      value:[],
      user:[],
      options: [
        {
          value: 'emergency',
          label: '应急管理',
          check: false
        },
        {
          value: 'way',
          label: '道路管理',
          check: false
        },
        {
          value: 'protection',
          label: '环保管理',
          check: false
        },
        {
          value: 'illegal',
          label: '违建管理',
          check: false
        }
      ],

      // 提交状态
      loading: false,
      // 是否是修改
      isUpdate: false
    };
  },
  watch: {
    data() {
      if (this.data) {
        this.form = Object.assign({}, this.data);
        this.isUpdate = true;

      } else {
        this.form = {id:0};
        this.isUpdate = false;
      }
    }
  },
  created() {
    this.getUserList()
  },
  methods: {
    async getUserList(){
      this.$http.get('/user/index').then(res => {
        this.user = res.data.data;
      })
    },
    changeArea() {
      this.form.unit_address = ''
      // const getLabel = function (val, arr, index = 0, res = []) {
      //   for (let i in arr) {
      //     if (arr[i].value === val[index]) {
      //       res.push(arr[i].label)
      //       if (index === val.length - 1) {
      //         return res
      //       } else {
      //         return getLabel(val, arr[i].children, ++index, res)
      //       }
      //     }
      //   }
      // }
    },
    /* 保存编辑 */
    save() {
      this.$refs['form'].validate((valid) => {
        if (valid) {
          this.form.model_type = this.form.model_type.join(',')
          this.loading = true;
          this.$http.post('/unit/edit', this.form).then(res => {
            this.loading = false;
            if (res.data.code === 0) {
              this.$message.success(res.data.msg);
              if (!this.isUpdate) {
                this.form = {};
              }
              this.updateVisible(false);
              this.$emit('done');
            } else {
              this.$message.error(res.data.msg);
            }
          }).catch(e => {
            this.loading = false;
            this.$message.error(e.message);
          });
        } else {
          return false;
        }
      });
    },
    isChecked(item) {
      if (item.check && this.form.model_type.indexOf(item.value) == -1) {
        this.form.model_type.push(item.value)
      } else if (!item.check) {
        this.form.model_type.forEach((elm, idx) => {
          if (elm == item.value) {
            this.form.model_type.splice(idx, 1)
          }
        })
      }
      this.$emit('selectedVal', this.form.model_type)
    },
    removeTag(value) {
      this.options.forEach((elm) => {
        if (elm.value == value) {
          elm.check = false
        }
      })
      this.$emit('selectedVal', this.form.model_type)
    },
    /* 更新visible */
    updateVisible(value) {
      this.$emit('update:visible', value);
    }
  }
}
</script>

<style scoped>
</style>
